package com.healthcloud.utils.file;

import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * 文件处理工具类
 *
 * @author yucl
 */
public class FileUtils {

    /**
     * linux保存路径
     */
    private final static String saveLinuxPath = File.separator + "usr" + File.separator + "local" + File.separator + "vap" + File.separator;
    /**
     * windows保存路径
     */
    private final static String saveWindowsPath = "C:\\temp\\";

    public static File saveFile(MultipartFile file, String path) throws IllegalStateException, IOException{
        File f = new File(path);
        file.transferTo(f);
        return f;
    }

    /**
     * 输出指定文件的byte数组
     *
     * @param filePath 文件路径
     * @param os       输出流
     * @return
     */
    public static void writeBytes(String filePath, OutputStream os) throws IOException {
        FileInputStream fis = null;
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                throw new FileNotFoundException(filePath);
            }
            fis = new FileInputStream(file);
            byte[] b = new byte[1024];
            int length;
            while ((length = fis.read(b)) > 0) {
                os.write(b, 0, length);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }

    /**
     * 删除文件
     *
     * @param filePath 文件
     * @return
     */
    public static boolean deleteFile(String filePath) {
        boolean flag = false;
        File file = new File(filePath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
            flag = true;
        }
        return flag;
    }

    /**
     * 根据url下载图片
     *
     * @param url 图片地址
     */
    public static byte[] downLoadPicture(String url) {
        URL urlInfo = null;
        try {
            urlInfo = new URL(url);
            DataInputStream dataInputStream = new DataInputStream(urlInfo.openStream());

            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            dataInputStream.close();
            return output.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new byte[1024];
    }

    /**
     * 根据url地址将图片下载到本地
     *
     * @param urlList url地址
     * @param path    保存路径
     */
    public static void downloadByUrlAndPath(String urlList, String path) {
        URL url = null;
        try {
            url = new URL(urlList);
            DataInputStream dataInputStream = new DataInputStream(url.openStream());

            FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
            ByteArrayOutputStream output = new ByteArrayOutputStream();

            byte[] buffer = new byte[1024];
            int length;

            while ((length = dataInputStream.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }
            fileOutputStream.write(output.toByteArray());
            dataInputStream.close();
            fileOutputStream.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建文件夹
     *
     * @param path
     */
    public static void isExist(String path) {

        File file = new File(path);
        //判断文件夹是否存在,如果不存在则创建文件夹
        if (!file.exists()) {
            boolean b = file.mkdirs();
        }
    }

    /**
     * 获取下载目录
     */
    public static String getFolderPath() {
        if (System.getProperties().getProperty("os.name").startsWith("Windows")) {
            return saveWindowsPath;
        } else if (System.getProperties().getProperty("os.name").startsWith("Mac")) {
            return "/tmp/";
        } else {
            return saveLinuxPath;
        }
    }

}